﻿var $edittr = null;
var ServiceId = GetQueryString("Id");
var dataSourceId = GetQueryString("dataSourceId");
$(function () {
    $("#DataServiceId").val(ServiceId);
    getServiceDetails();
    getDataSourceDetails();
    $("#UpdateColumn").click(function () {
        $("#update-column-modal").modal({ show: true });
    });
    //更新数据列
    $("#update-data").submit(function () {
        try {
            var trlist = $("#table-details-column tbody tr");
            if (trlist.length > 0) {
                var str = "";
                for (var i = 0; i < trlist.length; i++) {
                    var tempjson = JSON.parse($(trlist[i]).attr("data"));
                    str += (tempjson.name + ",");
                    str += (tempjson.type)+",";
                    if (i != (trlist.length - 1)) {
                        str += ",";
                    }
                }
                //修改   
                var url = "DataSourceDetails-WebService.html?Id=" + ServiceId;
                $.ajax({
                    url: apiUrl,
                    type: "Post",
                    dataType: "json",
                    data: {
                        "sys:sno": "ETL",
                        "sys:act": "UpdateDataSourceColumns",
                        "ServiceId": ServiceId,
                        "SourceId": dataSourceId,
                        "ColumnSettings": str
                    },
                    success: function (data) {
                        if (data.IsSccuess) {
                            ShowGritter("保存成功", 2, url);
                        } else {
                            if (IsLogined(data.ErrorCode)) {
                                alert("修改失败（" + "错误代码：" + data.ErrorCode + "，错误信息：" + data.Message + ")");
                            }
                        }
                    },
                    beforeSend: function () {
                        ShowGritter("提交中");
                    }
                });
            }
        } catch (e) {
            console.log(e);
        }
        return false;
    });
    //获取列数据
    $("#update-column-modal").submit(function () {
        var pars = $(".update-date-parameter");
        var str = "";
        for (var i = 0; i < pars.length; i++) {
            str += (JSON.parse($(pars[i]).parent().parent().attr("data")).name + ",");
            str += $(pars[i]).val();
            if (i != (pars.length - 1)) {
                str += ",";
            }
        }
        $.ajax({
            url: apiUrl,
            type: "Post",
            dataType: "json",
            data: {
                "sys:sno": "WS",
                "sys:act": "TestDataSource",
                "ServiceId": ServiceId,
                "SourceId": dataSourceId,
                "Parameters": str
            },
            success: function (data) {
                if (data.IsSccuess) {
                    AppadColumnData(data.Data.Items);
                    ShowGritter("更新完成，请提交");
                    console.log(JSON.stringify(data));
                } else {
                    if (IsLogined(data.ErrorCode)) {
                        alert("修改失败（" + "错误代码：" + data.ErrorCode + "，错误信息：" + data.Message + ")");
                    }
                }
            },
            beforeSend: function () {
                ShowGritter("更新中");
            }
        });
        return false;
    });
});
//组合参数数据  编辑时需调用此方法处理返回的数据
//传入的参数类型为对象数组
var AppadParameterData = function (data, option, id) {
    //var list = new Array();
    var trlist = "";
    for (var i = 0; i < data.length; i++) {
        var $this = data[i];
        //list.push({ name: $this.Name, id: $this.Id, type: $this.Type, description: $this.Description });
        trlist += AssembleParameterData({ name: $this.Name, id: $this.Id, type: $this.Type }, option);
    }
    $(id).append(trlist);
}
//传入参数对象返回一行的表格数据
var AssembleParameterData = function (data, option) {
    var tr = "<tr data='" + JSON.stringify(data) + "'><td>";
    tr += (data.name + "</td>");
    tr += ("<td>" + data.type + "</td>");
    if (option) {
        tr += ("<td><input type=\"text\" class=\"update-date-parameter\" name=\"update-date-" + data.name + "\" /></td>");
    }
    tr += "</tr>";
    return tr;
}


//组合列数据
//传入的参数类型为对象数组
var AppadColumnData = function (data) {
    //var list = new Array();
    var trlist = "";
    for (var i = 0; i < data.length; i++) {
        var $this = data[i];
        //list.push({ name: $this.Name, id: $this.Id, type: $this.Type, description: $this.Description });
        trlist += AssembleColumnData({ name: $this.Name, id: $this.Id, type: $this.Type });
    }
    $("#table-details-column").append(trlist);
}
//传入列对象返回一行的表格数据
var AssembleColumnData = function (data) {
    var tr = "<tr data='" + JSON.stringify(data) + "'><td>";
    tr += (data.name + "</td>");
    tr += ("<td>" + data.type + "</td></tr>");
    return tr;
}



//获取服务的详细信息
var getServiceDetails = function () {
    $.ajax({
        url: apiUrl,
        data: {
            "sys:sno": "WS",
            "sys:act": "GetDataService",
            "ServiceId": ServiceId
        },
        dataType: "json",
        success: function (data) {
            if (data.IsSccuess) {
                $("#DataSourceName").val(data.Data.Name);

            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert(data.Message);
                }
            }
        },
        beforeSend: function () {
            // $("btn_login").html("登录中...");
        }
    });
}
//获取数据源信息
var getDataSourceDetails = function () {
    $.ajax({
        url: apiUrl,
        data: {
            "sys:sno": "ETL",
            "sys:act": "GetDataSource",
            "DataServiceId": ServiceId,
            "DataSourceId": dataSourceId
        },
        dataType: "json",
        success: function (data) {
            if (data.IsSccuess) {
                var str = "";
                $("#WebServiceMethod").val(data.Data.Name);
                if (data.Data.Columns.length > 0) {
                    $("#UpdateColumn").hide();
                    $("#SubmitColumn").hide();
                    AppadColumnData(data.Data.Columns);//组装数据并并填充表格
                } else {
                    AppadParameterData(data.Data.Parameters, true, "#update-table-data");//组装数据并并填充表格
                }
                AppadParameterData(data.Data.Parameters, false, "#table-details-parameter");//组装数据并并填充表格
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert(data.Message);
                }
            }
        },
        beforeSend: function () {
            // $("btn_login").html("登录中...");
        }
    });
}
var UpdateColumnData = function () {

}